package com.ren.market.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.bean.OrderDto;
import com.ren.bean.SnowIdUtils;
import com.ren.market.bean.Ticket;
import com.ren.market.service.IMovieService;
import com.ren.market.service.ITicketService;
import com.ren.service.IOrderService;
import com.ren.service.TwoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ren
 * @since 2022-04-18
 */
@RestController
public class TicketController {
    @Autowired
    IMovieService movieService;
    @Autowired
    ITicketService ticketService;
    @Autowired
    TwoService twoService;
    @Autowired
    IOrderService orderService;
    @GetMapping("info/{tag}")
    public String info(@PathVariable("tag")int tag){
        return twoService.twoInfo(tag);
    }
    @GetMapping("getAllTicket")
    public List<Ticket> getAllTicket(){
        QueryWrapper<Ticket> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ticketTag",0);
        List<Ticket> list = ticketService.list(queryWrapper);
        System.out.println("list = " + list);
        return list;
    }
    @PostMapping("placeOrder")
    public String placeOrder(OrderDto orderDto){
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(date);
        orderDto.setOrdertime(format);
        orderDto.setOrderNum(SnowIdUtils.uniqueLong());
        orderDto.setAllsale(orderDto.getTicketsale()*orderDto.getNum());
        boolean b = orderService.placeOrder(orderDto);
        if(b){
            QueryWrapper<Ticket> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("ticketName",orderDto.getTicketname());
            Ticket one = ticketService.getOne(queryWrapper);
            boolean b1 = ticketService.saveOrUpdate(one);
            return "支付成功未出票";
        }else {
            return "支付未成功";
        }
    }
    @GetMapping("findOrder/{userCode}")
    public List<com.ren.market.vo.OrderDto> findOrder(@PathVariable("userCode")String userCode){
        List<OrderDto> order = orderService.findOrder(userCode);
        List<com.ren.market.vo.OrderDto> orderDtos=new ArrayList<>();
        for (OrderDto or:order) {
            com.ren.market.vo.OrderDto ord=new com.ren.market.vo.OrderDto();
            BeanUtils.copyProperties(or,ord);
            if(ord.getTag()==0){
                ord.setInfo("支付成功未出票");
            }else if(ord.getTag()==1){
                ord.setInfo("出票成功");
            }else if(ord.getTag()==2){
                ord.setInfo("出票失败");
            }
            orderDtos.add(ord);
        }
        return orderDtos;
    }

}
